package org.kzwl.service.web.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.advice.BizException;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.AdminBO;
import org.kzwl.comm.pojo.bo.DeleteAdminBO;
import org.kzwl.comm.pojo.bo.LoginAdminBO;
import org.kzwl.comm.pojo.bo.QueryAdminBO;
import org.kzwl.comm.pojo.dto.AdminLoginDTO;
import org.kzwl.comm.utils.MyUtils;
import org.kzwl.entity.web.Admin;
import org.kzwl.entity.web.Town;
import org.kzwl.mapper.web.AdminMapper;
import org.kzwl.mapper.web.TownMapepr;
import org.kzwl.service.web.AdminService;
import org.kzwl.service.web.TownService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    TownMapepr townMapepr;
    @Autowired
    TownService townService;

    @Override
    public ResponseData<String> login(LoginAdminBO loginAdminBO) {
        Admin admin = this.baseMapper.selectOne(new QueryWrapper<Admin>().lambda().eq(Admin::getCuAdminUsername, loginAdminBO.getCuUsername()));
        if (admin != null) {
            if (!MyUtils.createMd5(loginAdminBO.getCuUserPassword()).equals(admin.getCuAdminPwd())) {
                throw new BizException("密码错误");
            }
        } else {
            throw new BizException("用户不存在");
        }
        AdminLoginDTO adminLoginDTO = new AdminLoginDTO();
        adminLoginDTO.setCuAdminId(admin.getCuAdminId());
        adminLoginDTO.setCuAdminUsername(admin.getCuAdminUsername());
        adminLoginDTO.setCuOnlyId(admin.getCuOnlyId());
        adminLoginDTO.setCuParentId(admin.getCuParentId());
        Town town = this.townMapepr.selectById(admin.getCuOnlyId());
        if (town == null) {
            throw new BizException("场馆不存在");
        }

        adminLoginDTO.setCuTownName(town.getCuTownName());
        String jwt = MyUtils.createJwt(JSON.toJSONString(adminLoginDTO));
        return new ResponseData<>(jwt);
    }

    @Override
    public ResponseData<Void> saveIn(AdminBO adminBO) {
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminBO, admin);
        QueryWrapper<Admin> adminQueryWrapper = new QueryWrapper<Admin>();
        adminQueryWrapper.clear();
        adminQueryWrapper.lambda().eq(Admin::getCuOnlyId, adminBO.getCuOnlyId())
                .eq(Admin::getCuParentId, adminBO.getCuParentId());
        admin.setCuAdminPwd(MyUtils.createMd5(admin.getCuAdminPwd()));
        if (ObjectUtil.isNull(adminBO.getCuAdminId())) {
            if (adminBO.getCuParentId() != 0) {
                Admin selectOne = this.baseMapper.selectOne(adminQueryWrapper);
                if (ObjectUtil.isNotNull(selectOne)) {
                    throw new BizException("此场馆已经存在最高管理员");
                }
            }
            Admin one = this.baseMapper.selectOne(adminQueryWrapper.lambda().eq(Admin::getCuAdminUsername, adminBO.getCuAdminUsername()));
            if (ObjectUtil.isNotNull(one)) {
                throw new BizException("此用户名已存在");
            }
            this.baseMapper.insert(admin);
        } else {
            admin.setCuOnlyId(null);
            admin.setCuParentId(null);
            admin.setCuAdminUsername(null);
            this.baseMapper.updateById(admin);
        }
        return new ResponseData<>();
    }

    @Override
    public ResponseData<IPage<Admin>> queryPage(QueryAdminBO queryAdminBO) {
        Page<Admin> page = this.baseMapper.selectPage(new Page<>(queryAdminBO.getCurrent(), queryAdminBO.getSize()), new QueryWrapper<Admin>().lambda().eq(ObjectUtil.isNotNull(queryAdminBO.getCuOnlyId()), Admin::getCuOnlyId, queryAdminBO.getCuOnlyId()));
        page.getRecords().forEach(item ->{
            item.setCuAdminPwd(townService.list().stream().filter(i->i.getCuTownId() == item.getCuOnlyId()).collect(Collectors.toList()).get(0).getCuTownName());
        });
        return new ResponseData<>(page);
    }

    @Override
    public ResponseData<Void> deleteById(DeleteAdminBO deleteAdminBO) {
        if (deleteAdminBO.getCuOnlyId() != 1 || deleteAdminBO.getCuParentId() != 1)
        {
            throw new BizException("权限不足");
        }
        this.baseMapper.deleteById(deleteAdminBO.getCuAdminId());
        return new ResponseData<>();
    }
}
